<template>
  <div class="login">
      <div class="box1">
        <div class="box2">
            <h2>小U商城后台管理系统</h2>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="ruleForm.password" autocomplete="off" ></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
            </el-form>
        </div>
      </div>
      <!-- <el-row>
        <el-col :span="8" :offset="8">
          <div>
            <p>小U商城后台管理系统</p>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="ruleForm.pass" ></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="success" @click="submitForm('ruleForm')">立即登录</el-button>
            </el-form-item>
            </el-form>
          </div>
        </el-col>
      </el-row> -->
  </div>
</template>
<script>
import {mapMutations} from 'vuex'
import { userlogin } from '../../request';
  export default {
    data() {
      var userName = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('用户名不能为空'));
        }
        setTimeout(() => {
          if (value.length<5) {
            callback(new Error('用户名长度必须大于6'));
          } else {
            callback();
          }
        }, 1000);
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      return {
        ruleForm: {
          username:'',
          password: '',
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          username: [
            { validator: userName, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      ...mapMutations({
        saveUserInfo:"saveUserInfo"
      }),
      submitForm() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          // 校验成功
          let res=await userlogin(this.ruleForm)
          this.saveUserInfo(res)
          this.$router.push("/index");
        } else {
          // 检验失败
          console.log("error submit!!");
          return false;
        }
      });
    },
      resetForm() {
        this.$refs.ruleForm.resetFields();
      }
    }
  }
</script>
<style lang="" scoped>
    .login{
        background: url('../../assets/background.jpeg');
        height: 100%;
        width: 100%;
    }
    .box1{
        position: absolute;
        left: 50%;
        top: 50%;
        margin-left: -215px;
        margin-top: -140px;
        width: 450px;
        background-color: #fff;
        padding: 30px 30px 0 0;
        border-radius: 10px;
    }
    .box2{
        width: 400px;   
        text-align: center;
    }
    .box2 h2{
        color: red;
        line-height: 60px;
        padding-bottom: 10px;
    }
</style>